#pragma once
#include <list>
#include "GameObject.h"


typedef std::list<GameObject*> GameObjList;

// A generic object manager. Used for batch updates, rendering and destroying gameobjects
class GameObjectManager
{
public:
	GameObjList Objects;
	bool DeleteDeadObjects; // should we delete objects from memory as they die? if not, they
	// just get removed from Objects

	GameObjectManager(void)
	{
		//DeleteDeadObjects = true;
	}

	~GameObjectManager(void)
	{
		DestroyAllObjects();
	}

	virtual void Init()
	{
		// delete everything
		DestroyAllObjects();
	}

	virtual void Update()
	{
		for(GameObjList::iterator it = Objects.begin(); it != Objects.end();)
		{
			(*it)->Update();

			if((*it)->isDead)
			{
				GameObject* ge = (*it); // copy pointer

				it = Objects.erase(it); // delete pointer - iterator moves on

				//if(DeleteDeadObjects)
				delete ge; // delete object from memory
			}
			else
				it++;
		}
	}

	virtual void Render()
	{
		for(GameObjList::iterator i = Objects.begin(); i != Objects.end(); i++)
			(*i)->Render();
	}

	virtual void Add(GameObject* o) 
	{
		Objects.push_back(o); 
	}

	int Size() 
	{ 
		return (int)Objects.size(); 
	}

protected:
	// delete everything from memory
	void DestroyAllObjects()
	{
		for(GameObjList::iterator it = Objects.begin(); it != Objects.end();)
		{
			delete (*it);
			it = Objects.erase(it);
		}
	}

};
